import {
    deleteContent,getProjectList,finishTask,verifyCodeFun
  } from './taskListServ'
  // import { parse } from 'qs'
  import { message } from 'antd'
  // import moment from 'moment'
  import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
  import $ from 'jquery';
  
  const defultState = {
      biz:{
        queryForm: {}
      },
      submitLoading: false,
      submitFeikongLoading: false,
      backLoading: false,
      areaId: '',
      areaList: [],
      products: [],
      id:"",
      otherQuery:{
        areaId:"",
        projectId:""
      }
  }
  
  const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
      setup ({dispatch, history}) {
        history.listen(({ pathname, query }) => {
          if('' + tmpModule.pathname === '' + pathname){
            if(query.areaId){
              defultState.otherQuery.areaId = query.areaId;
              defultState.otherQuery.projectId = query.projectId;
            }
            dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
            dispatch({type: 'getProjectList', payload: {}})
          }
        })
      },
    },
  
  
    effects: {
      //获取区域项目
      *getProjectList({payload},{put,select,call}){
        let {areaList,areaId} = yield select(state => state[tmpModule.namespace])
        let params = {
            level : payload.areaCode?4:2,
            companyguid:payload.areaCode?payload.areaCode: ''
          }
          if(yield call(verifyCodeFun,{})){
          const { data } = yield call(getProjectList, {...params})
            let list = data
            if(!list.length){
              return
            }
            areaList =  list.map((item,index)=>{
              return {
                label: item.buname,
                value: item.buguid,
                children: item.childSysUninessunitList
              }
            })
            yield put({type: 'updateStore', payload: {areaList}})
            yield put({type: 'filterProjectList', payload: {}})
          }
        },
      *deleteContent({payload},{put,select,call}){
        if(payload.actionType == 'del'){
          let params = {
            id: payload.id
          }
          const { success, msg, data } = yield call(deleteContent, {...params})
          let defultState = yield select(state => state[tmpModule.namespace])
          if(success){
            message.success('删除成功')
            defultState.biz.refreshListDatas(true)
          }
        }
        if(payload.actionType == 'end'){
          let params = {
            id: payload.id
          }
          const { success, msg, data } = yield call(finishTask, {...params})
          let defultState = yield select(state => state[tmpModule.namespace])
          if(success){
            message.success('结束任务成功')
            defultState.biz.refreshListDatas(true)
          }
        }
      },
      *filterProjectList({payload},{put,select,call}){    
        let {products,areaList,biz} = yield select(state => state[tmpModule.namespace])
        let list = []
        for(var item of areaList){
          if(biz.queryForm.areaId&&item.value == biz.queryForm.areaId){
            list = item.children
          }
        }
        if(list.length){
          products =  list.map((item,index)=>{
            return {
              label: item.buname,
              value: item.buguid,
            }
          })
        }
        yield put({type: 'updateStore', payload: {products}})
      },
    },
    // 存入状态机
    reducers: {
      updateStore(preState, action) {
        return Object.assign({}, preState, action.payload)
      },
    }
  }
  
  export default tmpModule
  